<?php
/*
PluginName:文章分类
PluginUrl:http://www.lisijie.org
PluginAuthor:lisijie
PluginAuthorUrl:http://www.lisijie.org
PluginDesc:在博客的侧栏显示你的文章分类。
PluginVer:2.0.0
*/

class CategoryWidget extends Widget {
	
	function CategoryWidget() {
		$this->Widget(array('title'=>'文章分类'));
	}
	
	function setting() {
		echo '<p>标题：<input type="text" name="title" value="'.$this->get_option('title').'" /></p>
		<p>选项：
		<input type="checkbox" name="showcount" '.( $this->get_option('showcount') ? 'checked' : '').' id="category_showcount"><label for="category_showcount">显示文章数量</label>
		<input type="checkbox" name="showrss" '.( $this->get_option('showrss') ? 'checked' : '').' id="category_showrss"><label for="category_showrss">显示RSS图标</label><br />
		</p>';
	}
	
	function update($data) {
		$data['title'] = empty($data['title']) ? '文章分类' : check_str($data['title']);
		$data['showcount'] = $data['showcount'] ? 1 : 0;
		$data['showrss'] = $data['showrss'] ? 1 : 0;
		return $data;
	}
	
	function display() {	
		global $_CACHE;
		$data = '<ul class="widget_cates">';
		$cids = $_CACHE['cate']['blog'];
		foreach ( $cids as $key => $cid ) {
			$cate = $_CACHE['cate'][$cid];
			unset($cids[$key]);
			if ( $cate['parentid'] != 0 ) {
				continue;
			}
			$target = $count = $rssicon = '';
			if ( empty($cate['redirect']) ) {
				$url = url("mod=cate&id=$cid&alias={$cate[alias]}");	
				if ( $this->get_option('showcount') ) {
					$count = '<span>('.$cate['count'].')</span>';
				}
				if ( $this->get_option('showrss') ) {
					$rssicon = '<a href="'.config('blogdir').'rss.php?cate='.$cid.'" title="订阅该栏目"><img src="'.config('blogdir').'images/rss.png" /></a>';
				}
			} else {
				$url = $cate['redirect'];
				$target = 'target="_blank"';
			}
			if ( empty($cate['children']) ) {
				$data .= '<li><a href="'.$url.'" title="'.$cate['catename'].' '.$target.'">'.$cate['catename'].'</a>'.$count.$rssicon.'</li>';
			} else {
				$data .= '<li class="widget_parentcate"><a href="'.$url.'" title="'.$cate['catename'].'" '.$target.'>'.$cate['catename'].'</a>
				<ul class="widget_subcates">';
				foreach ( $cids as $skey => $sid ) {
					$subcate = $_CACHE['cate'][$sid];
					unset($cids[$skey]);
					$target = $count = $rssicon = '';
					if ( $subcate['parentid'] == $cid ) {
						if ( empty($subcate['redirect']) ) {
							$url = url("mod=cate&id=$sid&alias={$subcate[alias]}");
							if ( $this->get_option('showcount') ) {
								$count = ' <span>('.$cate['count'].')</span>';
							}
							if ( $this->get_option('showrss') ) {
								$rssicon = ' <a href="'.config('blogdir').'rss.php?cate='.$sid.'" title="订阅该栏目"><img src="'.config('blogdir').'images/rss.png" /></a>';
							}
						} else {
							$url = $subcate['redirect'];
							$target = 'target="_blank"';
						}
						$data .= '<li><a href="'.$url.'" title="'.$cate['catename'].' '.$target.'">'.$subcate['catename'].'</a>'.$count.$rssicon.'</li>';
					}
				}
				$data .= '</ul></li>';
			}
		}
		$data .= '</ul>';
		return $data;
	}
}

?>